<template>
  <div class="user-info-mobile-container">
    <div class="user-info-mobile-card">
      <div class="user-info-mobile-row avatar-row">
        <span>更换头像</span>
        <div
          class="avatar-circle"
          :style="{ background: userInfo.avatarColor || '#409EFF' }"
          @click="showColorDialog = true"
          style="cursor:pointer;"
        >
          <i class="el-icon-user" style="color:#fff;font-size:24px;"></i>
        </div>
      </div>
      <!-- 颜色选择弹窗 -->
      <el-dialog v-model="showColorDialog" title="选择头像颜色" width="340px" :close-on-click-modal="false">
        <div style="display:flex;flex-wrap:wrap;gap:16px;justify-content:center;padding:16px 0;">
          <div v-for="color in avatarColors" :key="color" :style="{
            width: '48px', height: '48px', borderRadius: '50%', background: color, border: selectedColor===color ? '3px solid #409eff' : '2px solid #eee', cursor: 'pointer', display:'flex',alignItems:'center',justifyContent:'center',transition:'border 0.2s'}"
            @click="selectColor(color)"></div>
        </div>
        <template #footer>
          <el-button @click="showColorDialog = false">取消</el-button>
          <el-button type="primary" @click="saveColor" :disabled="!selectedColor">保存</el-button>
        </template>
      </el-dialog>
      <div class="user-info-mobile-row">
        <span>手机号</span>
        <span class="user-info-mobile-value">{{ userInfo.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') }}</span>
      </div>
      <div class="user-info-mobile-row">
        <span>昵称</span>
        <span class="user-info-mobile-value" v-if="!editUsername" @click="editUsername = true">{{ userInfo.username }}</span>
        <template v-else>
          <el-input v-model="editUsernameValue" size="small" style="width: 120px; display: inline-block;" />
          <el-link type="success" @click="saveUsername" style="margin-left: 8px;">保存</el-link>
          <el-link type="info" @click="cancelEdit" style="margin-left: 4px;">取消</el-link>
        </template>
      </div>
    </div>
    <el-button-group class="user-info-btn-group" v-if="!props.hideActions">
      <el-button @click="goBack" type="primary" class="user-info-btn">返回主界面</el-button>
      <el-button @click="toggleTheme" class="user-info-btn">切换主题</el-button>
    </el-button-group>
  </div>
</template>

<script setup>
import { ref, onMounted, defineProps } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
const props = defineProps({ hideActions: Boolean })
const router = useRouter()
const userStore = useUserStore()
const userInfo = userStore.userInfo
const editUsername = ref(false)
const editUsernameValue = ref(userInfo.username)

// 头像颜色选择相关
const showColorDialog = ref(false)
const avatarColors = [
  '#409EFF', '#67C23A', '#E6A23C', '#F56C6C', '#909399', '#1abc9c', '#9b59b6', '#e67e22', '#e84393', '#00b894'
]
const selectedColor = ref(userInfo.avatarColor || avatarColors[0])
const selectColor = (color) => {
  selectedColor.value = color
}
const saveColor = () => {
  localStorage.setItem('avatarColor', selectedColor.value)
  userInfo.avatarColor = selectedColor.value
  userStore.setUserInfo(userInfo)
  ElMessage.success('头像颜色已更新')
  showColorDialog.value = false
}

// 页面加载时自动读取localStorage
onMounted(() => {
  const localColor = localStorage.getItem('avatarColor')
  if (localColor) {
    userInfo.avatarColor = localColor
    userStore.setUserInfo(userInfo)
    selectedColor.value = localColor
  }
})

const goBack = () => {
  router.push('/')
}
const saveUsername = async () => {
  try {
    const res = await request.post('/api/user/updateUsername', {
      userId: userInfo.id,
      username: editUsernameValue.value
    })
    if (res.code === 200) {
      userInfo.username = editUsernameValue.value
      userStore.setUserInfo(userInfo)
      editUsername.value = false
      ElMessage.success('昵称已修改')
    } else {
      ElMessage.error(res.message || '修改失败')
    }
  } catch (e) {
    ElMessage.error('修改失败')
  }
}
const cancelEdit = () => {
  editUsernameValue.value = userInfo.username
  editUsername.value = false
}
const toggleTheme = () => {
  // 主题切换逻辑
}
</script>

<style scoped>
.user-info-mobile-container {
  padding: 32px 0;
  min-height: 80vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  background: #fff;
}
.user-info-mobile-card {
  width: 420px;
  background: #fff;
  border-radius: 32px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.06);
  padding: 24px 0;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.user-info-mobile-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  padding: 18px 32px;
  border-bottom: 1px solid #f2f2f2;
}
.user-info-mobile-row:last-child {
  border-bottom: none;
}
.user-info-mobile-value {
  color: #888;
  cursor: pointer;
}
.avatar-row {
  justify-content: space-between;
}
.user-info-btn-group {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-top: 32px;
}
.user-info-btn {
  min-width: 140px;
  height: 44px;
  font-size: 18px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(64,158,255,0.06);
}
.avatar-circle {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(64,158,255,0.08);
  margin-left: 8px;
}
:deep(.el-dialog) {
  border-radius: 24px !important;
}
</style> 